package org.judo.generate.mvc;

public class ViewParser implements Parser
{
	ViewResult viewResult;
	ParseResults parseResults;
	private String curStatement;
	Parser nextParser;
	
	public ParseResults getParseResults()
	{
		return parseResults;
	}

	public void setParseResults(ParseResults parseResults)
	{
		this.parseResults = parseResults;
	}
	
	public Parser getNestedParser()
	{
		return nextParser;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/view"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{
		if (nextParser != null)
			return true;
		
		return false;
	}

	public void parseNextLine(String line) throws ParseException
	{
		nextParser = null;
		curStatement = line;
		
		if (line.startsWith("/view"))
		{
		}
		else if (line.equals("."))
		{
		}
		else if (line.equals(""))
		{
			viewResult.html += "\t\t\t<br>\n";
		}
		else if (line.startsWith("h1:"))
		{
			viewResult.html += "\t\t\t<h1>"+ParserUtil.getValue(line,viewResult)+"</h1>\n";
		}
		else if (line.startsWith("h2:"))
		{
			viewResult.html += "\t\t\t<h2>"+ParserUtil.getValue(line,viewResult)+"</h2>\n";
		}
		else if (line.startsWith("h3:"))
		{
			viewResult.html += "\t\t\t<h3>"+ParserUtil.getValue(line,viewResult)+"</h3>\n";
		}
		else if (line.startsWith("h4:"))
		{
			viewResult.html += "\t\t\t<h4>"+ParserUtil.getValue(line,viewResult)+"</h4>\n";
		}
		else if (line.startsWith("template:"))
		{
			viewResult.pageTemplate = ParserUtil.getValue(line,viewResult);
		}
		else if (line.startsWith("scope:"))
		{
			String scope = ParserUtil.getValue(line,viewResult);
			viewResult.scope = scope.trim();
		}
		else if (line.startsWith("menu:"))
		{
			String menu = ParserUtil.getValue(line,viewResult);
			menu = menu.trim();
			String parts[] = menu.split(",");
			
			if (parts.length != 2)
				throw new ParseException("Menu statements must be in the format: menu:[menu_Name],[display_name]");
			
			viewResult.menuName = parts[0];
			viewResult.menuDisplay = parts[1];
		}
		else if (line.startsWith("path:"))
		{
			String path = ParserUtil.getValue(line,viewResult);
			viewResult.path = path.trim();
		}
		else if (line.startsWith("form:"))
		{
			FormParser parser = new FormParser();
			nextParser = parser;
			parser.setViewResult(viewResult);
		}
		else if (line.startsWith("data_table:"))
		{
			DatatableParser parser = new DatatableParser();
			nextParser = parser;
			parser.setViewResult(viewResult);
		}
		else if (line.startsWith("display:"))
		{
			DisplayParser parser = new DisplayParser();
			nextParser = parser;
			parser.setViewResult(viewResult);
		}
		else if (line.startsWith("default_action:"))
		{
			viewResult.methodAnnotations.add("@DefaultAction");
			viewResult.imports.add("org.judo.controller.DefaultAction");
		}
		else if (line.startsWith("title:"))
		{
			String title = ParserUtil.getValue(line, viewResult);
			viewResult.title = title;
		}
		else if (line.startsWith("action:"))
		{
			ActionCodeParser parser = new ActionCodeParser();
			parser.setContainer(viewResult);
			nextParser = parser;
		}
		else
		{
			viewResult.html += "\t\t\t"+ParserUtil.parseFreeText(line,viewResult)+"<br>\n";
		}
	}

	public void setInitLine(String line) throws ParseException
	{
		viewResult = new ViewResult();
		viewResult.title = "New Page";
		
		String split[] = line.split(":");
		
		if (split.length == 1)
			throw new ParseException("View name not specified in init line");
		
		if (split[1].equals(""))
			throw new ParseException("View name not specified in init line");
		
		String name = split[1].trim();
		
		if (!name.contains("/"))
			throw new ParseException("View names must be in the format [controller_name].[view_name]");
		
		String parts[] = name.split("/");
		
		if (parts.length != 2)
			throw new ParseException("View names must be in the format [controller_name]/[view_name]");
		
		viewResult.actionName = parts[1].trim();
		viewResult.contollerName = parts[0].trim();
		
		if (parts[0].trim().equals("no_controller"))
		{
			viewResult.genConroller = false;
		}
		
		parseResults.views.put(name,viewResult);
	}
}
